﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProductManager
{
    public partial class AddProvider : Form
    {
        public AddProvider()
        {
            InitializeComponent();
        }

        private void SearchProvider()
        {
            DataTable products = DBAccess.Search("Provider", txtSearch.Text, cboSearch.SelectedItem.ToString());
            if (products == null)
            {
                return;
            }
            lvProvider.Items.Clear();
            foreach (DataRow row in products.Rows)
            {
                lvProvider.Items.Add(new ListViewItem(new string[] {
                    row["Name"].ToString(), row["Address"].ToString(),
                    row["Website"].ToString(), row["Rating"].ToString(),
                    row["ID"].ToString() }));
            }
        }

        private void AddProvider_Load(object sender, EventArgs e)
        {
            cboSearch.SelectedIndex = 0;
            SearchProvider();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            SearchProvider();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (lvProvider.SelectedItems.Count == 1)
            {
                ((ProductCategories)Owner).AddExistProvider(lvProvider.SelectedItems[0].SubItems[4].Text,
                    txtPrice.Text);
                txtPrice.Text = "";
            }
            else
            {
                MessageBox.Show("Please choose one provider!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
